package com.changwu.cwbase.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;

import com.blankj.utilcode.util.Utils;
import com.changwu.cwbase.Controllers;
import com.changwu.cwbase.bean.entity.User;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class AppUtil {
    private static Context context = Utils.getApp();

    public static List<User> getTempUser(int size) {
        if (size <= 0) {
            size = 3;
        }
        List<User> temps = new ArrayList<>();
        User temp = null;
        for (int i = 0; i < size; i++) {
            temp = new User();
            temps.add(temp);
        }
        return temps;
    }

    public static Object getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            return field.get(object);
        } catch (Exception e) {
            return null;
        }
    }

    public static String getDownPath() {
        return Controllers.downDir + System.currentTimeMillis() + ".png";
    }

    public static Bitmap getBitmap(View v) {
        Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        v.draw(canvas);
        return bitmap;
    }

    public static String getWebUrl(String path) {
        if (!path.startsWith(Controllers.httpPrefix)) {
            path = Controllers.httpPrefix + path;
        }
        return path;
    }

    private static long lastClickTime;

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        // 两次点击间隔小于500毫秒
        if (time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * @param times
     * @return
     * @方法说明:防止控件被重复点击，如果点击间隔时间小于指定时间就点击无� * @方法名称:isFastDoubleClick
     * @返回�boolean
     */
    public static boolean isFastDoubleClick(long times) {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < times) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
}
